//


import UIKit

 protocol WP_PhotoViewDelegate:NSObjectProtocol {
    func updateloading()
}
class WP_PhotoView: UIView {
    
    var btn:UIButton!
     var img:UIImageView!
    weak var delegate:WP_PhotoViewDelegate?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        customUI()
    }
    
    func customUI() {
        
        btn = UIButton()
        btn.setTitle("load", for: .normal)
        btn.setTitleColor(.black, for: .normal)
        btn.addTarget(self, action: #selector(click), for: .touchUpInside)
        self.addSubview(btn)
        btn.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(kRatioHeight(20))
            make.left.equalToSuperview().offset(kRatioWidth(12))
            make.height.equalTo(kRatioHeight(40))
            
        }
        
//        img = UIImageView()
//        self.addSubview(img)
//        img.snp.makeConstraints { (make) in
//            make.top.equalTo(btn.snp.bottom).offset(kRatioHeight(20))
//            make.left.equalToSuperview().offset(kRatioWidth(12))
//            make.height.width.equalTo(kRatioHeight(40))
//        }
        
        self.addSubview(collectionView)
    }
    
    @objc func click()  {
        delegate?.updateloading()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /*
     // Only override draw() if you perform custom drawing.
     // An empty implementation adversely affects performance during animation.
     override func draw(_ rect: CGRect) {
     // Drawing code
     }
     */
    
    lazy var collectionView:UICollectionView = {
        
        let layout = UICollectionViewFlowLayout.init()
        layout.minimumLineSpacing = 5
        layout.minimumInteritemSpacing = 5
        layout.scrollDirection = .vertical
        layout.sectionInset = UIEdgeInsets.init(top: 0, left: 5, bottom: 0, right: 5)
        layout.itemSize = CGSize(width: (kScreenWidth - 25) / 4, height: (kScreenWidth - 25) / 4)
        //
        let collection = UICollectionView(frame: CGRect(x: 0, y: 70, width: kScreenWidth, height: kRatioHeight(200)), collectionViewLayout: layout)
        collection.delegate = self
        collection.dataSource = self
        collection.backgroundColor = UIColor.white;
        collection.register(WP_PhotoCollectionViewCell.classForCoder(), forCellWithReuseIdentifier: "cell")
        
        return collection
    }()
    lazy var imgArray:[UIImage] = {
        let arr = Array<UIImage>()
        return arr
    }()
    
    func reloadData(arr:Array<UIImage>) {
        imgArray = arr
        collectionView.reloadData()
    }
    
}


extension WP_PhotoView :UICollectionViewDataSource, UICollectionViewDelegate{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imgArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell:WP_PhotoCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! WP_PhotoCollectionViewCell
        
        cell.img.image = imgArray[indexPath.row]
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
       return CGSize(width: (kScreenWidth - 25) / 4, height: (kScreenWidth - 25) / 4)
    }
   
    
}
